﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PerfumeShopManagement.Utility;
using PerfumeShopManagement.DAL;
using PerfumeShopManagement.Model;
using PerfumeShopManagement.BLL;

namespace PerfumeShopManagement.View
{
    public partial class frmCategoryManagement : Form
    {
        BllCategory bllCategory;

        public frmCategoryManagement()
        {
            this.bllCategory = new BllCategory();
            InitializeComponent();
        }

        public frmCategoryManagement(BllCategory bllCategory)
        {
            this.bllCategory = bllCategory;
            InitializeComponent();
        }

        private void resetFieldWithoutId()
        {
            // Reset without id
            txbCategoryName.Text = "";
        }

        private void setNextId()
        {
            txbCategoryId.Text = Constants.AUTO_NUMBERING_MESSAGE;
        }

        private void reloadGridView()
        {
            // Load grid view
            object[] buffer = new object[2];
            List<DataGridViewRow> rows = new List<DataGridViewRow>();
            List<Category> categories = bllCategory.GetAll();
            foreach (Category item in categories)
            {
                buffer[0] = item.CategoryId;
                buffer[1] = item.CategoryName;

                rows.Add(new DataGridViewRow());
                rows[rows.Count - 1].CreateCells(gridCategories, buffer);
            }
            gridCategories.Rows.Clear();
            gridCategories.Rows.AddRange(rows.ToArray());
        }

        private void btnAddCategory_Click(object sender, EventArgs e)
        {
            setNextId();
            resetFieldWithoutId();
        }

        private void btnDeleteCategory_Click(object sender, EventArgs e)
        {
            if (gridCategories.SelectedRows.Count > 0)
            {
                bool allDeleted = true;
                foreach (DataGridViewRow item in gridCategories.SelectedRows)
                {
                    int id = (int)item.Cells["colCategoryId"].Value;
                    if (!bllCategory.DeleteById(id))
                        allDeleted = false;
                }

                if (allDeleted)
                {
                    reloadGridView();
                    MessageBox.Show(Constants.SUCCESSFULLY_MESSAGE);
                }
                else
                {
                    MessageBox.Show(Constants.FAILED_MESSAGE);
                }
            }
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            resetFieldWithoutId();
        }

        private void btnSaveCategory_Click(object sender, EventArgs e)
        {
            Category category = new Category();
            if (NumberUtil.IsNumeric(txbCategoryId.Text))
                category.CategoryId = int.Parse(txbCategoryId.Text);
            category.CategoryName = txbCategoryName.Text;

            if (bllCategory.Save(category))
            {
                reloadGridView();
                MessageBox.Show(Constants.SUCCESSFULLY_MESSAGE);
            }
            else
            {
                MessageBox.Show(Constants.FAILED_MESSAGE);
            }
        }

        private void btnEditCategory_Click(object sender, EventArgs e)
        {
            if (gridCategories.SelectedRows.Count > 0)
            {
                DataGridViewRow item = gridCategories.SelectedRows[0];
                txbCategoryId.Text = ((int)item.Cells["colCategoryId"].Value).ToString();
                txbCategoryName.Text = (string)item.Cells["colCategoryName"].Value;
            }
        }

        private void frmCategoryManagement_Load(object sender, EventArgs e)
        {
            setNextId();
            resetFieldWithoutId();
            reloadGridView();
        }
    }
}
